home *** CD-ROM | disk | FTP | other *** search
- /*
- * 2004
- * Copyright Jeremy Gillick, All rights reserved
- */
-
- /*
- *
- * ERROR CODES
- * 100 - 149: switchproxy_setProxy()
- * 150 - 199: switchproxy_removeProxy()
- * 200 - 249: switchproxy_clearCookies()
- * 250 - 299: switchproxy_editProxyDialog()
- * 300 - 349: switchproxy_manualUpgradCheck()
- *
- */
-
- var gSwitchP_List;
- var gSwitchP_ListPopup;
- var gSwitchP_ContextList;
- var gSwitchP_StatusBar;
- var gSwitchP_MenuList;
- var gSwitchP_ManageList;
- var gSwitchP_ElementList;
- var gSwitchP_ElementButton;
- var gSwitchP_LastItem;
- var gSwitchP_ProxyCount = 0;
- var gSwitchP_InManager = false;
- var gSwitchP_Loaded = false;
- var gSwitchP_Cycle = null;
- var gSwitchP_CommandEnd = true;
- var gSwitchP_Prefs = null;
- var gSwitchP_NoneLabel = "None";
-
- var gSwitchPTabMgr = null;
-
- function switchproxy_initProxy(event){
-
- window.removeEventListener("load", switchproxy_initProxy, true);
-
- if(!gSwitchP_Loaded){
- /*if(typeof(switchproxy_TabManager) != 'undefined')
- gSwitchPTabMgr = new switchproxy_TabManager(); */
-
- gSwitchP_List = document.getElementById('proxy-list')
- gSwitchP_ListPopup = document.getElementById('proxy-list-popup');
- gSwitchP_ManageList = document.getElementById('manage-proxy-list');
- gSwitchP_ContextList = document.getElementById('context-proxy-list');
- gSwitchP_StatusBar = document.getElementById('switchproxy-status');
- gSwitchP_MenuList = document.getElementById('switchproxy-menu-list');
- gSwitchP_ElementList = document.getElementById('switchproxy-element-list');
- gSwitchP_ElementButton = document.getElementById('switchproxy-element-button');
- gSwitchP_Prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
-
- gSwitchP_NoneLabel = switchproxy_getString("common.proxy.none");
-
- //Is in Proxy Manager
- if(gSwitchP_ManageList != null){
- gSwitchP_InManager = true;
- gSwitchP_List = gSwitchP_ManageList;
- }
-
- switchproxy_showMenus(true);
-
- //Cleanup RDF File
- switchproxy_cleanupRdf();
-
- //Populate Proxy List
- switchproxy_populateList();
-
- //Set Last Proxy Selected
- if(gSwitchP_List != null)
- gSwitchP_LastItem = gSwitchP_List.selectedItem;
-
- //Add Preferences Listener
- var oProxyObserver = {
- observe : function(subject, topic, data){ switchproxy_populateList(); }
- };
- var oMenuObserver = {
- observe : function(subject, topic, data){ switchproxy_showMenus(false); }
- };
- var oPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
- //oPrefBranch.addObserver("network.proxy", oPrefObserver, false);
- oPrefBranch.addObserver("switchproxy.proxy.rdf.lastupdate", oProxyObserver, false);
- oPrefBranch.addObserver("switchproxy.display", oMenuObserver, false);
-
- //Check for updates (after 3 second delay)
- setTimeout("switchproxy_autoUpdate()", 3000);
-
- //Auto-Update Anonymous Proxy Lists (after 10 minutes)
- gAnonUpdateTimout = setTimeout("switchproxy_anon_autoUpdateLists()", 600000);
-
- // Start Anon Rotation
- setTimeout("switchproxy_anon_nextProxy()", 1000);
-
- gSwitchP_Loaded = true;
- }
- else {
- /*if(gSwitchPTabMgr)
- gSwitchPTabMgr.tabLoading();*/
- }
- }
-
- // Return Tab Manager
- function switchproxy_getTabManager(){
- return gSwitchPTabMgr;
- }
-
- //Used for debugging
- function switchproxy_debugEcho(str){
- if(str)
- alert(str)
- else
- alert("test");
- }
-
- //Displays the context menu based on what
- // the user selects in the options
- function switchproxy_showMenus(onStartup){
- try{
- //Show/Hide menus
- var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
-
- // Status Bar
- var isHidden = (oPrefs.prefHasUserValue("switchproxy.display.statusbar") && !oPrefs.getBoolPref("switchproxy.display.statusbar"))
- document.getElementById("switchproxy-status").setAttribute("collapsed", isHidden);
-
- // If not thunderbird
- if(navigator.userAgent.search(/Thunderbird/gi) < 0){
-
- //Context
- var isHidden = (oPrefs.prefHasUserValue("switchproxy.display.context") && !oPrefs.getBoolPref("switchproxy.display.context"))
- document.getElementById("switchproxy-context-menu").setAttribute("collapsed", isHidden);
- document.getElementById("switchproxy-context-separator").setAttribute("hidden", isHidden);
-
- //Toolbar
- if(onStartup){
- oPrefs.setBoolPref("switchproxy.display.toolbar", (!document.getElementById("proxy-toolbar").getAttribute("collapsed")));
- }
- else{
- var isHidden = (oPrefs.prefHasUserValue("switchproxy.display.toolbar") && !oPrefs.getBoolPref("switchproxy.display.toolbar"))
- document.getElementById("proxy-toolbar").setAttribute("collapsed", isHidden);
- }
- }
-
- }catch(err){}
- }
-
- // Show Element List
- function switchproxy_showElementList(event){
- var oContext = document.getElementById('context-proxy-list');
- var sUri = oContext.selectedItem.value;
- var sType = oContext.selectedItem.getAttribute("proxyType");
-
- // Anonymous Proxy Menu Item
- if(sType == "3"){
- document.getElementById('switchproxy-menu-anon').setAttribute("collapsed", false);
- document.getElementById('switchproxy-menu-anon-seperator').setAttribute("collapsed", false);
-
- document.getElementById('switchproxy-context-anon').setAttribute("collapsed", false);
- document.getElementById('switchproxy-context-anon-seperator').setAttribute("collapsed", false);
-
- document.getElementById('switchproxy-tools-anon').setAttribute("collapsed", false);
- document.getElementById('switchproxy-tools-anon-seperator').setAttribute("collapsed", false);
- }
- else{
- document.getElementById('switchproxy-menu-anon').setAttribute("collapsed", true);
- document.getElementById('switchproxy-menu-anon-seperator').setAttribute("collapsed", true);
-
- document.getElementById('switchproxy-context-anon').setAttribute("collapsed", true);
- document.getElementById('switchproxy-context-anon-seperator').setAttribute("collapsed", true);
-
- document.getElementById('switchproxy-tools-anon').setAttribute("collapsed", true);
- document.getElementById('switchproxy-tools-anon-seperator').setAttribute("collapsed", true);
- }
- }
-
- //Returns is the entered menu is currently showing
- // values are 'context' or 'toolbar'
- function switchproxy_menuShowing(sMenu){
- var oObj = null;
-
- if(sMenu == "context"){
- oObj = document.getElementById("switchproxy-context-menu");
- }
- else if(sMenu == "toolbar"){
- oObj = document.getElementById("proxy-toolbar");
- }
-
- if(oObj == null)
- return false;
-
- return !eval(oObj.getAttribute("collapsed"));
- }
-
-
- //Clears current Proxy List
- function switchproxy_clearList(){
-
- gSwitchP_ProxyCount = 0;
-
- //Toolbar List
- if(!gSwitchP_InManager && gSwitchP_List != null && gSwitchP_List.removeAllItems && !gSwitchP_List.open){
- gSwitchP_List.removeAllItems();
- }
- //Manage Proxy Box
- if(gSwitchP_InManager && gSwitchP_List != null){
- aNodes = gSwitchP_List.childNodes;
- for(var i = aNodes.length-1; i >= 0; i--){
- gSwitchP_List.removeChild(aNodes[i]);
- }
- }
- //Context List
- if(gSwitchP_ContextList != null){
- aNodes = gSwitchP_ContextList.childNodes;
- for(var i = aNodes.length-1; i >= 0; i--){
- gSwitchP_ContextList.removeChild(aNodes[i]);
- }
- }
- //Toolbar Element List
- if(gSwitchP_ElementList != null){
- aNodes = gSwitchP_ElementList.childNodes;
- for(var i = aNodes.length-1; i >= 0; i--){
- if(aNodes[i].getAttribute("class") == "proxy-menu-item")
- gSwitchP_ElementList.removeChild(aNodes[i]);
- }
- }
- // Tools Menu List
- if(gSwitchP_MenuList != null){
- aNodes = gSwitchP_MenuList.childNodes;
- for(var i = aNodes.length-1; i >= 0; i--){
- if(aNodes[i].getAttribute("class") == "proxy-menu-item")
- gSwitchP_MenuList.removeChild(aNodes[i]);
- }
- }
- }
-
- //Add Item to lists
- function switchproxy_appendToList(sLabel, sValue, isSelected, iType){
- var oItem = null;
-
- gSwitchP_ProxyCount++;
-
- try{
-
- //Toolbar List
- if(!gSwitchP_InManager && gSwitchP_List != null && !gSwitchP_List.open){
- oItem = gSwitchP_List.appendItem(sLabel, sValue);
- oItem.setAttribute("oncommand", "switchproxy_anon_stopRotation(); switchproxy_selectProxy();");
- oItem.setAttribute("proxyType", iType);
- }
- //Manage Proxy Box
- if(gSwitchP_InManager && gSwitchP_List != null && sLabel != gSwitchP_NoneLabel){
- oItem = gSwitchP_List.appendItem(sLabel, sValue);
- oItem.setAttribute("proxyType", iType);
- }
- //Context List
- if(gSwitchP_ContextList != null){
- oItem = document.createElement("menuitem");
- oItem.setAttribute("label", sLabel);
- oItem.setAttribute("value", sValue);
- oItem.setAttribute("type", "checkbox");
- oItem.setAttribute("proxyType", iType);
- oItem.setAttribute("oncommand", "switchproxy_queueSetProxy(this);");
- gSwitchP_ContextList.appendChild(oItem);
- }
- //Toolbar Element List
- if(gSwitchP_ElementList != null){
- //Get separator, which is after proxy list
- var oSeparator = document.getElementById("switchproxy-element-list-separator");
- oItem = document.createElement("menuitem");
- oItem.setAttribute("id", sValue);
- oItem.setAttribute("label", sLabel);
- oItem.setAttribute("value", sValue);
- oItem.setAttribute("type", "checkbox");
- oItem.setAttribute("proxyType", iType);
- oItem.setAttribute("class", "proxy-menu-item");
- oItem.setAttribute("oncommand", "switchproxy_queueSetProxy(this);");
- gSwitchP_ElementList.insertBefore(oItem, oSeparator);
- }
- // Tools Menu List
- if(gSwitchP_MenuList != null){
- //Get separator, which is after proxy list
- var oSeparator = document.getElementById("switchproxy-menu-list-separator");
- oItem = document.createElement("menuitem");
- oItem.setAttribute("id", sValue);
- oItem.setAttribute("label", sLabel);
- oItem.setAttribute("value", sValue);
- oItem.setAttribute("type", "checkbox");
- oItem.setAttribute("proxyType", iType);
- oItem.setAttribute("class", "proxy-menu-item");
- oItem.setAttribute("oncommand", "switchproxy_queueSetProxy(this);");
- gSwitchP_MenuList.insertBefore(oItem, oSeparator);
- }
-
- //Select
- if(isSelected)
- switchproxy_selectItem(sLabel);
-
- }catch(err){}
- }
-
- //Sets List item with labe sLabel, to be selected/checked
- function switchproxy_selectItem(sLabel){
-
- try{
- //Toolbar List
- if(!gSwitchP_InManager && gSwitchP_List != null){
- oItem = gSwitchP_List.getElementsByAttribute("label", sLabel);
- if(oItem != null){
- oItem = oItem[0];
- gSwitchP_List.selectedItem = oItem;
- gSwitchP_List.label = oItem.getAttribute("label");
- oItem.setAttribute("selected", true);
- }
- }
- //Context
- if(gSwitchP_ContextList != null){
- oItem = gSwitchP_ContextList.getElementsByAttribute("label", sLabel);
- if(oItem != null){
- //Uncheck Other
- var aChecked = gSwitchP_ContextList.getElementsByAttribute("checked", "true");
- for(var c = 0; c < aChecked.length; c++){
- aChecked[c].setAttribute("checked", false);
- }
-
- //Select
- oItem = oItem[0];
- gSwitchP_ContextList.selectedItem = oItem;
- oItem.setAttribute("checked", true);
- }
- }
- //Toolbar Element List
- if(gSwitchP_ElementList != null){
- oItem = gSwitchP_ElementList.getElementsByAttribute("label", sLabel);
- if(oItem != null && oItem.length > 0){
- //Uncheck Other
- var aChecked = gSwitchP_ElementList.getElementsByAttribute("checked", "true");
- for(var c = 0; c < aChecked.length; c++){
- aChecked[c].setAttribute("checked", false);
- }
-
- //Check Selected
- oItem = oItem[0];
- gSwitchP_ElementList.selectedItem = oItem;
- oItem.setAttribute("selected", true);
- oItem.setAttribute("checked", true);
-
- //Change Button Label
- gSwitchP_ElementButton = document.getElementById('switchproxy-element-button');
- if(gSwitchP_ElementButton != null){
- gSwitchP_ElementButton.setAttribute("label", sLabel);
- }
- }
- }
- // Tools Menu List
- if(gSwitchP_MenuList != null){
- oItem = gSwitchP_MenuList.getElementsByAttribute("label", sLabel);
- if(oItem != null && oItem.length > 0){
-
- //Uncheck Other
- var aChecked = gSwitchP_MenuList.getElementsByAttribute("checked", "true");
- for(var c = 0; c < aChecked.length; c++){
- aChecked[c].setAttribute("checked", false);
- }
-
- //Check Selected
- oItem = oItem[0];
- gSwitchP_MenuList.selectedItem = oItem;
- oItem.setAttribute("selected", true);
- oItem.setAttribute("checked", true);
- }
- }
- // Status Bar
- if(gSwitchP_StatusBar != null){
- gSwitchP_StatusBar.setAttribute("label", "Proxy: "+ sLabel);
- }
- switchproxy_clearTimeout(gSwitchP_Cycle);
-
- }catch(err){}
- }
-
- //Return menuitem for the given sUri
- function switchproxy_getMenuItem(sUri){
- var oItem = null;
-
- //Toolbar
- oItem = (gSwitchP_List != null) ? gSwitchP_List.getElementsByAttribute("value", sUri) : null;
- if(oItem != null && oItem[0] != null)
- return oItem[0];
-
- //Context
- oItem = (gSwitchP_List != null) ? gSwitchP_ContextList.getElementsByAttribute("value", sUri) : null;
- if(oItem != null && oItem[0] != null)
- return oItem[0];
-
- //Toolbar Element
- oItem = (gSwitchP_List != null) ? gSwitchP_ElementList.getElementsByAttribute("value", sUri) : null;
- if(oItem != null && oItem[0] != null)
- return oItem[0];
-
-
- //Default
- return null;
- }
-
- function switchproxy_cycle(sAction){
- if(gSwitchP_Cycle != null){
-
- switchproxy_clearTimeout(gSwitchP_Cycle);
- gSwitchP_CommandEnd = true;
-
- if(sAction == 'change' && gSwitchP_LastItem != null && gSwitchP_List != null && gSwitchP_List.selectedItem.label != gSwitchP_LastItem.label && gSwitchP_List.selectedIndex > 0){
- gSwitchP_List.label = gSwitchP_LastItem.label;
- gSwitchP_List.selectedItem = gSwitchP_LastItem;
- gSwitchP_List.selectedItem.value = gSwitchP_LastItem.value;
-
- switchproxy_setProxy();
- }
- else if(sAction == 'select' && gSwitchP_List != null && gSwitchP_List.selectedIndex > -1){
- switchproxy_setStatus(switchproxy_getString("toolbar.notApplied"), "#F00");
- setTimeout("switchproxy_setStatus(switchproxy_getString('toolbar.loading'))", 3000);
- setTimeout("switchproxy_populateList('"+ gSwitchP_LastItem.label +"');", 4000);
- setTimeout("switchproxy_setProxy();", 4500);
- }
-
- }
- }
-
- //When user selects a Proxy from the menu list
- function switchproxy_selectProxy(){
-
- switchproxy_clearTimeout(gSwitchP_Cycle);
- if(gSwitchP_List == null || gSwitchP_List.selectedItem == null || gSwitchP_InManager)
- return;
-
- if(gSwitchP_List.selectedItem.label != gSwitchP_NoneLabel){
- document.getElementById('edit-button').disabled = false;
- document.getElementById('remove-button').disabled = false;
- document.getElementById('edit-button').image = "chrome://switchproxy/content/icons/pencil.png";
- document.getElementById('remove-button').image = "chrome://switchproxy/content/icons/cross.png";
-
- }
- else{
- document.getElementById('edit-button').disabled = true;
- document.getElementById('remove-button').disabled = true;
- document.getElementById('edit-button').image = "chrome://switchproxy/content/icons/pencil_disabled.png";
- document.getElementById('remove-button').image = "chrome://switchproxy/content/icons/cross_disabled.png";
- }
-
- if(typeof(gSwitchP_List.selectedItem.value) != 'undefined'){
- //If this item is not being used by the browser,
- // show a helpful message and cycle
- var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
- if((!oPrefs.prefHasUserValue("switchproxy.proxy.current") || oPrefs.getCharPref("switchproxy.proxy.current") != gSwitchP_List.selectedItem.value) && gSwitchP_LastItem != null && gSwitchP_LastItem != gSwitchP_List.selectedItem){
- switchproxy_setStatus(switchproxy_getString("toolbar.clickApply"), "#060");
-
- //Cycle if user doesn't click 'Apply'
- if(gSwitchP_Cycle == null)
- gSwitchP_Cycle = setTimeout("switchproxy_cycle('select')", 10000);
- }
- //If it is being used by the browser
- else if(oPrefs.prefHasUserValue("switchproxy.proxy.current") && oPrefs.getCharPref("switchproxy.proxy.current") == gSwitchP_List.selectedItem.value){
- switchproxy_clearTimeout(gSwitchP_Cycle);
-
- //Anonomous
- if(gSwitchP_List.selectedItem.getAttribute("proxyType") == "3"){
- if(switchproxy_anon_getCurrent() == "unknown")
- switchproxy_setProxy(false);
-
- switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label +" ["+ switchproxy_anon_getCurrent() +"] ("+ switchproxy_anon_getCurrentCount() +")");
- }
- //Standard
- else{
- switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label);
- }
- }
- else{
- switchproxy_clearTimeout(gSwitchP_Cycle);
- switchproxy_setStatus(switchproxy_getString("toolbar.using.none"));
- }
- }
-
- return true;
- }
-
- //Queue Set Proxy
- function switchproxy_queueSetProxy(oMenuItem){
- switchproxy_clearTimeout(gSwitchP_Cycle);
- setTimeout("switchproxy_setProxy(true, true, null, '"+ oMenuItem.label +"', '"+ oMenuItem.value +"', '"+ oMenuItem.getAttribute("proxyType") +"')", 10);
- }
-
- //Set Proxy
- function switchproxy_setProxy(bClean, fromContextMenu, oMenuItem, sLabel, sUri, sType){
-
- //Start Command
- gSwitchP_CommandEnd = false;
- switchproxy_clearTimeout(gSwitchP_Cycle);
- switchproxy_anon_stopRotation();
-
- //If in Proxy Manager, do nothing
- if(gSwitchP_InManager){
- gSwitchP_CommandEnd = true;
- return;
- }
-
- var hasError = false;
-
- //Set fromContextMenu
- if(fromContextMenu == null)
- fromContextMenu = false;
-
- //Get Proxy URI & Label
- var sProxyUri = "";
- var sProxyLabel = "";
- var sProxyType = 0;
- if(sUri != null && sLabel != null){
- sProxyUri = sUri;
- sProxyLabel = sLabel;
-
- if(sType) sProxyType = sType;
- }
- else if(oMenuItem != null){
- sProxyUri = oMenuItem.getAttribute("value");
- sProxyLabel = oMenuItem.getAttribute("label");
- sProxyType = oMenuItem.getAttribute("proxyType");
- }
- else{
- if(fromContextMenu == true){
- sProxyUri = gSwitchP_ContextList.selectedItem.value;
- sProxyLabel = gSwitchP_ContextList.selectedItem.label;
- sProxyType = gSwitchP_ContextList.selectedItem.getAttribute("proxyType");
- }
- else if(gSwitchP_List != null){
- sProxyUri = gSwitchP_List.selectedItem.value;
- sProxyLabel = gSwitchP_List.selectedItem.label;
- sProxyType = gSwitchP_List.selectedItem.getAttribute("proxyType");
- }
- }
-
- //Branch if Anonomous
- if(sProxyType == "3"){
- switchproxy_setStatus(switchproxy_getString("toolbar.loading"));
- switchproxy_anon_loadProxy(sProxyUri, true);
- }
- else if(sProxyUri != ""){
- switchproxy_setStatus(switchproxy_getString("toolbar.loading"));
-
- try{
- //Change Browser Preferences
- var aProps = switchProxy_ds_getPropertyValuesFor(sProxyUri);
-
- for(key in gSwichProxy_options){
-
- //Get pref type and update preference
- var sPrefVal = "";
- if(typeof(gSwichProxy_options_defaults[key]) == "number"){
- gSwitchP_Prefs.setIntPref(gSwichProxy_options[key], parseInt(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
- }
- else if(typeof(gSwichProxy_options_defaults[key]) == "boolean"){
- gSwitchP_Prefs.setBoolPref(gSwichProxy_options[key], eval(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
- }
- else{ //String
- gSwitchP_Prefs.setCharPref(gSwichProxy_options[key], aProps[gSProxyRdfNodeUriRoot +"#"+ key]);
- }
- }
-
- //Get Proxy Label
- sProxyLabel = aProps[gSProxyRdfNodeName];
-
- //Update Status
- switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ sProxyLabel);
-
- //Update Preference
- gSwitchP_Prefs.setCharPref("switchproxy.proxy.current", sProxyUri);
- }
- catch(err){
- alert("100: "+ switchproxy_getString("error.unknown")+ "\n("+ err +")");
- hasError = true;
-
- //Cycle back to 'None'
- if(gSwitchP_Cycle == null && !fromContextMenu)
- gSwitchP_Cycle = setTimeout("switchproxy_cycle('change')", 500);
- }
- }
- else{
- //Turn off proxy pref
- try{
- gSwitchP_Prefs.setIntPref("network.proxy.type", 0);
-
- //Update Status
- switchproxy_setStatus(switchproxy_getString("toolbar.using.none"));
-
-
- //Update Preference
- gSwitchP_Prefs.setCharPref("switchproxy.proxy.current", "");
-
- }catch(e){
- alert("101: "+ switchproxy_getString("error.unknown"));
- hasError = true;
- }
- }
-
- // Finalize
- if(hasError){
- //Update Status
- switchproxy_setStatus(switchproxy_getString("error.proxy.load") +" " + sProxyLabel, "#F00");
- }
- else{
-
- //switchproxy_tab_notify(sProxyUri);
-
- //Select this proxy in all lists
- switchproxy_selectItem(sProxyLabel);
-
- //Set Last Proxy
- gSwitchP_LastItem = switchproxy_getMenuItem(sProxyUri);
-
- //Options
- try{
- if(bClean){
- // Clear Cookies
- if(typeof(gBrowser) != 'undefined' && gSwitchP_Prefs.prefHasUserValue("switchproxy.clear.cookies") && gSwitchP_Prefs.getBoolPref("switchproxy.clear.cookies")){
- switchproxy_clearCookies();
- }
-
- // Reload Page
- if((typeof(gBrowser) != 'undefined' && (!gSwitchP_Prefs.prefHasUserValue("switchproxy.reload.tab") || gSwitchP_Prefs.getBoolPref("switchproxy.reload.tab")))){
- try{
- var oTab = gBrowser.mCurrentBrowser;
- oTab.webNavigation.reload(nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE);
- } catch(err) { }
- }
-
- // Set Update Pref
- gSwitchP_Prefs.setIntPref("switchproxy.proxy.rdf.lastupdate", (new Date()).getTime());
- }
-
- }catch(err){alert("102: "+ switchproxy_getString("error.unknown") + "\n("+err+")");}
- }
-
- //End Command
- gSwitchP_CommandEnd = true;
-
- }
-
- //Test if the RDF Element, oRef, is the current proxy being used
- function switchproxy_isSelected(oRef){
-
- try{
- var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
- var aProps = switchProxy_ds_getPropertyValuesFor(oRef.Value);
- var sCurrUri = "";
- var iType = parseInt(aProps[gSProxyRdfNodeUriRoot + "#networkProxyType"]);
-
- if(oPrefs.prefHasUserValue("switchproxy.proxy.current") && (sCurrUri = oPrefs.getCharPref("switchproxy.proxy.current")) == oRef.Value){
-
- //Verify this is really what's being used in the browser
- if(iType == oPrefs.getIntPref("network.proxy.type")){
- if(iType == 1){
- for(key in gSwichProxy_options){
-
- //Skip These Keys
- if(key == "networkProxyType" || key == "networkProxyAutoconfigURL")
- continue;
-
- //Skip if RDF has Default Values
- if( (key == "networkProxySOCKSVersion" && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "5" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == ""))
- || (key == "networkProxyNone" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "localhost, 127.0.0.1")){
- continue;
- }
-
- //If Pref and RDF doesn't have a value for this
- if(!oPrefs.prefHasUserValue(gSwichProxy_options[key])
- && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "0")){
- continue;
- }
-
- // If pref doesn't have this and the RDF record does
- // return false
- if(!oPrefs.prefHasUserValue(gSwichProxy_options[key])
- && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "0")){
- return false;
- }
-
-
- //Get pref and convert to string
- var sPrefVal = "";
- if(oPrefs.getPrefType(gSwichProxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_INT)
- sPrefVal = oPrefs.getIntPref(gSwichProxy_options[key]) + "";
- if(oPrefs.getPrefType(gSwichProxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_BOOL)
- sPrefVal = oPrefs.getBoolPref(gSwichProxy_options[key]) + "";
- if(oPrefs.getPrefType(gSwichProxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_STRING)
- sPrefVal = oPrefs.getCharPref(gSwichProxy_options[key]);
-
- //Return false if isn't equal
- if(sPrefVal != aProps[gSProxyRdfNodeUriRoot +"#"+ key]){
- return false;
- }
- }
- return true;
- }
- //If type is 2 and the PAC url is the same
- else if(iType == 2 && oPrefs.prefHasUserValue("switchproxy.proxy.current")
- && oPrefs.getCharPref("network.proxy.autoconfig_url") == aProps[gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL"]){
- return true;
- }
- }
- //Anonomous
- else if(iType == 3 && oPrefs.prefHasUserValue("network.proxy.http") && oPrefs.prefHasUserValue("network.proxy.http_port")){
- var sProxy = oPrefs.getCharPref("network.proxy.http") +":"+ oPrefs.getIntPref("network.proxy.http_port");
-
- //is sProxy in proxy list
- oTestRes = switchProxy_ds_getElementForValue(gSProxyRdfNodeUriRoot +"#proxy", sProxy);
-
- if(oTestRes instanceof Components.interfaces.nsIRDFResource && oTestRes.Value == oRes.Value){
- switchproxy_anon_loadProxy(oRef.Value, true);
- return true;
- }
-
- return false;
- }
- }
-
- }catch(err){}
-
-
- return false;
- }
-
- function switchproxy_removeProxy(){
- switchproxy_clearTimeout(gSwitchP_Cycle);
- var oItem = gSwitchP_List.selectedItem;
-
- //Can't delete 'None'
- if(gSwitchP_List.selectedItem != null && gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel){
- alert("150: "+ switchproxy_getString("error.remove.forbidden"));
- return;
- }
-
- if(!confirm(switchproxy_getString("confirm.remove")))
- return;
-
- try{
- //RDF
- //Remove
- switchProxy_ds_removeElement(oItem.value);
-
- //Cleanup
- switchproxy_cleanupRdf();
-
- //Update List
- switchproxy_populateList();
- setTimeout("switchproxy_setProxy();", 300);
- }
- catch(err){
- alert("151: "+ switchproxy_getString("error.remove") + "\n("+ err +")");
- }
- }
-
- function switchproxy_clearCookies(){
- try{
- var oCookies = Components.classes["@mozilla.org/cookiemanager;1"].createInstance(Components.interfaces.nsICookieManager);
- oCookies.removeAll();
- }catch(err){
- alert("200: "+ switchproxy_getString("error.cookie"));
- }
- return true;
- }
-
-
- function switchproxy_populateList(sSelectedLabel){
- switchproxy_clearTimeout(gSwitchP_Cycle);
-
- var sProxyUri = "";
- var iSelected = (gSwitchP_List != null) ? gSwitchP_List.selectedIndex : -1;
- var aProxies = new Array();
-
- //Don't populate if a local command has not finished or if list is open
- if(!gSwitchP_CommandEnd){
- gSwitchP_CommandEnd = true; //reset
- return;
- }
- else{ //Start Command
- gSwitchP_CommandEnd = false;
- }
-
- //Load Elements
- try{
-
- //Remove List Items & Add 'None'
- switchproxy_clearList();
- switchproxy_appendToList(gSwitchP_NoneLabel, "", true, 0);
- switchproxy_selectItem(gSwitchP_NoneLabel);
-
- //Get and Sort Elements
- var aProxies = switchProxy_ds_getAllElements();
- aProxies.sort(switchproxy_sortProxies);
-
- //Add to list
- for(i = 0; i < aProxies.length; i++){
-
- if(typeof(aProxies[i]) == 'undefined')
- continue;
-
- try{
-
- oRes = switchProxy_ds_getResource(aProxies[i]);
- sProxyName = switchProxy_ds_getValueFor(oRes, switchProxy_ds_getResource(gSProxyRdfNodeName));
- sProxyType = switchProxy_ds_getValueFor(oRes, switchProxy_ds_getResource(gSProxyRdfNodeUriRoot+ "#networkProxyType"));
-
- if(sProxyName != null){
- oItem = switchproxy_appendToList(sProxyName, aProxies[i], false, sProxyType);
-
- //Select Item
- if((sSelectedLabel != null && sSelectedLabel == sProxyName) || (sSelectedLabel == null && switchproxy_isSelected(oRes))){
- switchproxy_selectItem(sProxyName);
- }
- }
-
- } catch(err) {}
- }
-
- //Finish
- gSwitchP_CommandEnd = true;
- setTimeout("switchproxy_selectProxy();", 300);
-
- }catch(err){}
- }
-
- /*
- * Sort Proxy List
- * + oResA and oResB are RDF URIs
- */
- function switchproxy_sortProxies(oResA, oResB){
- try{
- //Get Proxy Names
- sValA = switchProxy_ds_getValueFor(switchProxy_ds_getResource(oResA), switchProxy_ds_getResource(gSProxyRdfNodeName));
- sValB = switchProxy_ds_getValueFor(switchProxy_ds_getResource(oResB), switchProxy_ds_getResource(gSProxyRdfNodeName));
-
-
- if (sValA < sValB)
- return -1;
- if (sValA == sValB)
- return 0;
- if (sValA > sValB)
- return 1;
- }
- catch(e){ return 0; }
- }
-
- /* Cleanup unused SwitchProxy data in RDF
- * + Remove resources that have not been applied or used
- */
- function switchproxy_cleanupRdf(){
-
- try{
- swithproxy_upgrade_rdf();
-
- var aElements = switchProxy_ds_getAllElements();
- for(var e = 0; e < aElements.length; e++){
- oRes = switchProxy_ds_getResource(aElements[e]);
- aProps = switchProxy_ds_getPropertyValuesFor(aElements[e]);
-
- //If does not have a name property,
- // then it is not listed -- so it's junk
- if(aProps[gSProxyRdfNodeName] == null){
- switchProxy_ds_removeElement(aElements[e]);
- }
- }
- }catch(err){}
- }
-
- // Automatic Upgrade
- /*
- * From Version 0.4
- * + Add preference 'switchproxy.version'
- * + Add preference 'switchproxy.proxy.current'
- * + Change proxy's URI to random number
- * + Convert #proxy to networkProxyAutoconfigURL
- * + Add elements for full manual proxy configuration
- * + view the globals.js gSwichProxy_options array for full list
- */
- function swithproxy_upgrade_rdf(){
-
- try{
-
- //Object
- var oUriRegExp = new RegExp("\\/sp_proxy_[0-9]{5,6}$");
- var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
- var aElements = switchProxy_ds_getAllElements();
-
-
- //If perference version is current version, don't upgrade
- if(oPrefs.prefHasUserValue("switchproxy.version") && oPrefs.getCharPref("switchproxy.version") == gSwitchProxy_Version){
- return;
- }
-
- //Set Version
- oPrefs.setCharPref("switchproxy.version", gSwitchProxy_Version);
-
- //Loop through elements
- for(var e = 0; e < aElements.length; e++){
- var oRes = switchProxy_ds_getResource(aElements[e]);
- var aAttrs = switchProxy_ds_getPropertiesFor(aElements[e]);
-
- //(v0.4) Change '#proxy' URI
- if(aAttrs != null && typeof(aAttrs["proxy"]) != 'undefined' && aAttrs["proxy"] instanceof Components.interfaces.nsIRDFResource){
- switchProxy_ds_renamePropertyUri(oRes, aAttrs["proxy"], gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL");
- }
-
- //(v0.4) Add new Properties
- for(sAttrName in gSwichProxy_options){
- var oProp = switchProxy_ds_getResource(gSProxyRdfNodeUriRoot +"#"+ sAttrName);
- var sValue = gSwichProxy_options_defaults[sAttrName]; //Default value
-
- if(sValue != null)
- switchProxy_ds_addProperty(oRes, oProp, sValue, false);
- }
-
- //(v0.4) Change URI and ID property to random number
- if(!oUriRegExp.test(aElements[e])){
- var sUri = switchproxy_getUniqueProxyUri();
- switchProxy_ds_changePropertyValue(oRes, gSProxyRdfNodeId, sUri);
- switchProxy_ds_changeElementUri(oRes, sUri);
- }
-
- }
-
- //Preferences
- //(v0.4) Add 'switchproxy.proxy.current'
- if(!oPrefs.prefHasUserValue("switchproxy.proxy.current")){
- //Get & Set selected Proxy
- if(oPrefs.prefHasUserValue("network.proxy.autoconfig_url")){
- var sProxyUrl = oPrefs.getCharPref("network.proxy.autoconfig_url");
- var oRes = switchProxy_ds_getElementForValue(gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL", sProxyUrl);
-
- if(oRes != null){
- oPrefs.setCharPref("switchproxy.proxy.current", oRes.Value);
- }
- }
- }
-
- }catch(err){}
- }
-
- //Generates random number for proxy URI
- function switchproxy_getUniqueProxyUri(){
- var sUri = gSProxyRdfRoot + "/sp_proxy_" + Math.round((Math.random() * 200000));
-
- if(switchProxy_ds_doesProxyElementExist(sUri))
- return switchproxy_getUniqueProxyUri();
-
- return sUri;
- }
-
- function switchproxy_setStatus(sMsg, sColor){
- if(!sColor)
- sColor = "#000";
-
- oStatus = document.getElementById('status-text');
-
- if(oStatus != null){
- document.getElementById('status-text').style.color = sColor;
- document.getElementById('status-text').value = sMsg;
- }
- }
-
- //Clear Timeout
- function switchproxy_clearTimeout(){
- clearTimeout(gSwitchP_Cycle);
- gSwitchP_Cycle = null;
- }
-
- //Execute a command when system is ready
- // iCurrCount is for internal use, do not
- // pass this argument
- function switchproxy_doCommand(sCommand, iLimitCount){
-
- if(iLimitCount == null)
- iLimitCount = 0;
-
- //Wait for current command to end
- if(!gSwitchP_CommandEnd && iLimitCount < 10){
- iLimitCount++
- setTimeout("switchproxy_doCommand('"+ sCommand +"', "+ iCurrCount +")", 100);
- }
- else{
- gSwitchP_CommandEnd = true;
- setTimeout(sCommand, 100);
- }
- }
-
- /*
- * Launch Dialogs
- */
-
- //Edit Proxy Dialog
- function switchproxy_editProxyDialog(isNew){
- switchproxy_clearTimeout(gSwitchP_Cycle);
-
- var sAction = null;
-
- //Nothing Selected
- var oEditItem = null
- if(!isNew && (oEditItem = gSwitchP_List.selectedItem) == null){
- alert("250: "+ switchproxy_getString("error.edit.select"));
- return;
- }
-
- //Can't edit 'None'
- if(!gSwitchP_InManager && !isNew && gSwitchP_List != null && gSwitchP_List.selectedItem != null && gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel){
- alert("251: "+ switchproxy_getString("error.edit.forbidden"));
- return;
- }
-
- //Edit
- if(!isNew){
-
- //Anon
- if(oEditItem.getAttribute("proxyType") == "3"){
- window.openDialog("chrome://switchproxy/content/dialogs/editanon.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
- }
- //Standard
- else{
- window.openDialog("chrome://switchproxy/content/dialogs/editproxy.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
- }
- }
- //Add
- else{
- window.openDialog("chrome://switchproxy/content/dialogs/addproxytype.xul","editproxy","centerscreen, chrome", "add");
- }
- }
-
- //Manage Proxy Dialog
- function switchproxy_openProxyManager(){
- window.openDialog("chrome://switchproxy/content/dialogs/manager.xul","manageproxies","centerscreen, chrome, resizable");
- }
-
- /*
- * Options
- */
- function switchproxy_openSwitchProxyPrefs(){
- window.openDialog("chrome://switchproxy/content/options/options.xul","switchProxyPrefs","centerscreen, chrome");
- }
-
- function switchproxy_goToSwitchProxySite(fromDialog){
-
- // If Thunderbird
- if(navigator.userAgent.search(/Thunderbird/gi) > -1){
- var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();
- messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger);
- messenger.launchExternalURL(gSwitchProxy_DownloadSite +"?r=switchproxy");
- }
- else{
- opener.gBrowser.selectedTab = opener.getBrowser().addTab(gSwitchProxy_DownloadSite +"?r=switchproxy");
- }
- self.close();
- }
-
- function switchproxy_goToSwitchProxyAbout(){
- window.openDialog("chrome://switchproxy/content/dialogs/about.xul","switchProxyAbout","centerscreen, chrome, modal");
- }
-
- /*
- * Automatic Updator
- */
-
- function switchproxy_autoUpdate(){
-
- try{
-
- //Alert User
- var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
- if(!oPrefs.prefHasUserValue("switchproxy.update.notify") || oPrefs.getBoolPref("switchproxy.update.notify")){
-
- //Has 24 hours past since last notification
- var iYesterday = ((new Date()).getTime() / 1000) - 86400;
- if( (!oPrefs.prefHasUserValue("switchproxy.update.notify.time") || oPrefs.getIntPref("switchproxy.update.notify.time") <= iYesterday)
- && (sXpi = switchproxy_needsUpgrade()) != false){
-
- //Update timestamp
- oPrefs.setIntPref("switchproxy.update.notify.time", ((new Date()).getTime() / 1000));
-
- //Show Notification Dialog
- window.openDialog("chrome://switchproxy/content/dialogs/update.xul","switchProxyUpdate","centerscreen, chrome, modal", sXpi.Value);
- }
-
- }
-
- }catch(err){}
- }
-
- //If there is a new version, returns the XPI url, else returns false
- function switchproxy_needsUpgrade(){
-
- const sThisVersion = gSwitchProxy_Version;
- const sRdfUri = "http://jgillick.nettripper.com/plugins.rdf?p=switchProxy";
- const sProductUri = "http://jgillick.nettripper.com/rdf/switchproxy";
-
- try{
- // Get current version and XPI from remote RDF
- var oRdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
- var oDs = oRdf.GetDataSourceBlocking(sRdfUri);
- var oProduct = oRdf.GetResource(sProductUri);
-
- var sVersion = oDs.GetTarget(oProduct, oRdf.GetResource("http://jgillick.nettripper.com/rdf#version"), true);
- var sXpi = oDs.GetTarget(oProduct, oRdf.GetResource("http://jgillick.nettripper.com/rdf#xpi"), true);
-
- //Not literals
- if( !(sVersion instanceof Components.interfaces.nsIRDFLiteral)
- || !(sXpi instanceof Components.interfaces.nsIRDFLiteral)){
- return false;
- }
- //No Update
- else if(sThisVersion >= sVersion.Value){
- return false
- }
- else{
- return sXpi;
- }
- }catch(err){ return false; }
- }
-
- //The user selects to check for updates
- function switchproxy_manualUpgradCheck(){
-
- var sXpi = switchproxy_needsUpgrade();
-
- if(sXpi != false){
- if(confirm(switchproxy_getString("upgrade.install"))){
- try{
- var aInstall = new Array();
- aInstall["SwitchProxy"] = sXpi;
- InstallTrigger.install(aInstall, null);
-
- }catch(err){ alert("300: "+ switchproxy_getString("error.update") +"\n"+err); }
- }
- }
- else{
- alert(switchproxy_getString("upgrade.none"));
- }
-
- }
-
- /*
- * UNINSTALL
- */
- function switchproxy_uninstall(oLogger){
- try{
- var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
- oPrefs.deleteBranch("switchproxy");
-
- oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.SUCCESS_STATUS, "Preferences Branch 'switchproxy'", null, null));
- }catch(err){
- oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.WARN_STATUS, "Preferences Branch 'switchproxy'", 1001, err));
- throw 1001;
- }
- }
-
-